package com.uroad.liaoningpos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.ums.AppHelper;
import com.uroad.liaoningpos.R;
import com.uroad.liaoningpos.common.BaseActivity;
import com.uroad.lib.util.log.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @创建者 wlw
 * @创建时间 2019/4/4 15:01
 * @描述 余额查询
 */
public class BalanceQueryActivity extends BaseActivity {
    private String TAG = "BalanceQueryActivity";
    @BindView(R.id.tvBalance)
    TextView tvBalance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_balance_query);
        setTitle("余额查询");
        ButterKnife.bind(this);
        balanceQuery();
    }

    /**余额查询*/
    private void balanceQuery() {
        JSONObject json = null;
        try {
            json = new JSONObject("{}");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AppHelper.callTrans(this, "公共资源", "余额查询", json);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (AppHelper.TRANS_REQUEST_CODE == requestCode) {
            LogUtil.d(TAG, "resultCode" + resultCode);
            if (Activity.RESULT_OK == resultCode) {
                if (null != data) {
                    Log.i(TAG,"requestCode----->"+requestCode+"\nresultCode----->"+resultCode+"\ndata Extra(result)"+ data.getStringExtra("result"));
                    getPayResult(data);
                }else{
                    LogUtil.i(TAG,"Intent is null" + "\n");
                }
            }else{
                LogUtil.i(TAG,"resultCode is not RESULT_OK" + "\n");
            }
        }
    }
    private void getPayResult(Intent data) {
        StringBuilder result = new StringBuilder();
        Map<String, String> map = AppHelper.filterTransResult(data);
        result.append("{\"" + AppHelper.TRANS_APP_NAME + "\":\"" + map.get(AppHelper.TRANS_APP_NAME) + "\",");
        result.append("\"" + AppHelper.TRANS_BIZ_ID + "\":\"" + map.get(AppHelper.TRANS_BIZ_ID) + "\",");
        result.append("\"" + AppHelper.RESULT_CODE + "\":\"" + map.get(AppHelper.RESULT_CODE) + "\",");
        result.append("\"" + AppHelper.RESULT_MSG + "\":\"" + map.get(AppHelper.RESULT_MSG) + "\",");
        result.append("\"" + AppHelper.TRANS_DATA + "\":" + map.get(AppHelper.TRANS_DATA) + "}");
        LogUtil.i(TAG,"余额查询结果："+result.toString());
        tvBalance.setText("余额：0:00");
    }
}
